package org.example.yolodemo.service;

import org.example.yolodemo.entity.ImageDetection;
import org.example.yolodemo.repository.ImageDetectionRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImageDetectionService {
    private final ImageDetectionRepository repository;

    public ImageDetectionService(ImageDetectionRepository repository) {
        this.repository = repository;
    }

    public ImageDetection saveDetectionResult(String imageName, float x1, float y1, float x2, float y2) {
        ImageDetection detection = new ImageDetection();
        detection.setImageName(imageName);
        detection.setX1(x1);
        detection.setY1(y1);
        detection.setX2(x2);
        detection.setY2(y2);
        return repository.save(detection);
    }

    public List<ImageDetection> getAllDetections() {
        return repository.findAll();
    }

    public void deleteDetection(Long id) {
        repository.deleteById(id);
    }

    public ImageDetection updateDetection(Long id, String imageName, float x1, float y1, float x2, float y2) {
        ImageDetection detection = repository.findById(id).orElseThrow(() -> new RuntimeException("Detection not found"));
        detection.setImageName(imageName);
        detection.setX1(x1);
        detection.setY1(y1);
        detection.setX2(x2);
        detection.setY2(y2);
        return repository.save(detection);
    }
}